import { HTTP } from '../../../utils/http.js'
import api from '../../../utils/api.js'
import { config } from '../../../utils/config'
import utils from "../../../utils/util"
let http = new HTTP()
var app = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		orderType: false, //选择订单类型
		imgUrl: config.imgUrl,
		orderList: [],
		total: 0,
		pageNum: 1,
		pageSize: 10,
		payBtn: true,
		inputDelete: false,
		inputValue: '',
		type: 0,
		isfind: false, //是否查找过 缺省状态判断
		orderItem: [
			{ type: 0, typeName: '全部' },
			{ type: 1, typeName: '退款中' },
			{ type: 2, typeName: '已退款' }
		],
		orderClass: [
			{ name: '积分商城', path: '/pages/refund/pomList/pomList' },
			{ name: '地标商城', path: '/pages/refund/landmarkList/landmarkList' }
		]
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let type = options.type
		if (type) {
			this.setData({
				type
			})
			let orderItem = this.data.orderItem;
			let typeObj = orderItem.find(item => {
				return item.type == type
			})
			wx.setNavigationBarTitle({
				title: typeObj.typeName,
			})
		}
		this.getOrderList();
	},
	// 选择订单类型
	orderType() {
		this.setData({
			orderType: !this.data.orderType
		})
	},
	getOrderList() {
		let that = this;
		wx.showLoading({ title: '加载中' })
		http.request({
			url: api.refundList,
			method: 'GET',
			data: {
				page: that.data.pageNum,
				type: that.data.type
			},
			success: (res) => {
				console.log('refundList', res.data)
				let result = res.data
				that.setData({
					total: result.total,
					orderList: that.data.orderList.concat(result.data),
					totalPage: result.last_page,
					isfind: true
				})
				wx.hideLoading();
			},
			fail: (res) => {

			},
			complete: () => {

			}
		})
	},

	changeOrderType(e) {
		let index = e.currentTarget.dataset.index;
		let orderClass = this.data.orderClass;
		let hrefPath = orderClass[index]
		wx.redirectTo({
			url: hrefPath.path
		})
	},
	//根据状态查询订单
	anyOrder(e) {
		let orderType = e.currentTarget.dataset.type;
		let orderItem = this.data.orderItem;
		let typeObj = orderItem.find(item => {
			return item.type == orderType
		})
		this.setData({
			type: orderType,
			pageNum: 1,
			orderList: [],
			orderType: false,
			isfind: false
		})
		this.getOrderList();

	},

	// 点击订单详情
	orderDetailHandle(e) {
		let order_no = e.currentTarget.dataset.order;
		wx.navigateTo({
			url: '/pages/refund/refundDetail/refundDetail?order=' + order_no
		})
	},
	// 取消订单
	cancelOrder(e) {
		let that = this;
		let order_no = e.currentTarget.dataset.order;
		wx.showModal({
			title: '提示',
			content: '确定取消订单',
			confirmColor: '#EA5D56',
			success(res) {
				if (res.confirm) {

					http.request({
						url: api.cancelOrder,
						method: "POST",
						data: {
							order_no
						},
						success: (res) => {
							console.log(res);
							if (res.code == 200) {
								let orderList = that.data.orderList;
								orderList.forEach(item => {
									if (item.order_num == order_no) {
										item.status_text = '订单已取消';
										item.status = 0;
										item.is_cancellable = false;
										item.is_canbedeleted = true;
									}
								})
								that.setData({
									orderList
								})
							}
						},
						fail: (res) => {

						}
					})

				} else if (res.cancel) {
					return;
				}
			}
		})


	},



	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function (options) {
		let that = this

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	// /**
	//  * 页面相关事件处理函数--监听用户下拉动作
	//  */
	// onPullDownRefresh: function() {
	//     console.log('下拉刷新');
	//     if (this.data.pageNum < this.data.totalPage) {
	//         this.setData({
	//             pageNum: pageNum + 1
	//         })
	//         this.getOrderList();
	//     }
	//     wx.stopPullDownRefresh();

	// },

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		console.log('触底刷新');
		if (this.data.pageNum < this.data.totalPage) {
			this.setData({
				pageNum: this.data.pageNum + 1
			})
			this.getOrderList();
		}
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})